package com.ericsson.entironment.interfaces.message;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ericsson.common.date.AppliedDate;
import com.ericsson.common.db.DatabaseMng;
import com.ericsson.entironment.bean.TerminalBean;
import com.ericsson.entironment.sms.SMSHandler;


//通用消息类
public class CommonMessage
{
	protected String terminalseq;//终端序列号
	protected Vector<String> values=new Vector<String>();//各帧数据值
	private static Log log=LogFactory.getLog(CommonMessage.class);
	public String getTerminalseq()
	{
		return terminalseq;
	}
	public void setTerminalseq(String terminalseq)
	{
		this.terminalseq = terminalseq;
	}
	public Vector<String> getValues()
	{
		return values;
	}
	public void addValues(String value)
	{
		this.values.add(value);
	}
	
	public void save()
	{//保存信息中的内容
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		String date;
		long time;
		int i;
		String alert="";
		TerminalBean term=new TerminalBean();
		try
		{
			
			conn = DatabaseMng.getInstance().getConnection();
			
			stmt = conn.createStatement();
			date=AppliedDate.getInstance().getDate();
			time=(new Date()).getTime();
			term.load(this.terminalseq);
			//取出所有的通道值
			for(i=0;i<values.size();i++)
			{
				sql="insert into EMA_History(trmSequence,frameid,framename,framevalue,framedate,frametime) " +
				   "values ('"+this.terminalseq+"','"+i+"','"+term.getFrameName(""+i)+"','"+values.get(i)+"',"+
				   "'"+date+"','"+time+"')";
				stmt.executeUpdate(sql);
				
				if (checkLimit(values.get(i),i,this.terminalseq)==true)
				{
					alert+=term.getFrameName(""+i)+"超过门限范围;";
				}
			}
			
			//更新终端最后一次上报时间
			sql="update ema_terminal set lastmsgtime="+time+",trmstatus=1 where trmSequence='"+this.terminalseq+"'";
			stmt.executeUpdate(sql);
			
			//判断是否超门限，如果超过发送短信告警
			if (!alert.equals(""))
			{//修改终端为告警状态
				sql="update ema_terminal set trmstatus=2,alertcontent='"+alert+"' where trmSequence='"+this.terminalseq+"'";
				stmt.executeUpdate(sql);
			 //发送告警短信
				SMSHandler.getInstance().sendMessage(alert,getAllOptr());
			}
			
			
		}
		catch (Exception e)
		{
			log.error("execute sql error["+sql+"]",e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		
		
	}
	private String[] getAllOptr()
	{
		String target="";
		
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="select * from ema_operator";
			rst=stmt.executeQuery(sql);
			while(rst.next())
			{
				target=rst.getString("optrtel")+",";
			}

		}
		catch (Exception e)
		{
			log.error("获取操作员密码错误",e);
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		
		
		return target.split(",");
		
	}
	private boolean checkLimit(String value, int i,String seq)
	{
		Connection conn = null;
		Statement stmt = null;
		ResultSet rst = null;
		String sql = "";
		float max=0;
		float min=0;
		float rel=Float.parseFloat(value);
		try
		{
			conn = DatabaseMng.getInstance().getConnection();
			stmt = conn.createStatement();
			sql="select * from ema_valuelimit where frameid='"+i+"' and trmsequence='"+seq+"'";
			rst=stmt.executeQuery(sql);
			while (rst.next())
			{
				if (rst.getString("framename").indexOf("上")!=-1)
				{
					max=rst.getFloat("framevalue");
				}
				else
				{
					min=rst.getFloat("framevalue");
				}
			}
			
			if (max>0 && min>0)
			{
				if (rel<max && rel>min)
				{//正常
					return false;
				}
				else
				{//告警
					return true;
				}
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (rst != null)
					rst.close();
				if (stmt != null)
					stmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception e)
			{

			}

		}
		return false;
	}

}
